//

// (  )
//

package com.tools.acceleratecore.xlib.tool.impl;

import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog.Calls;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import com.tools.acceleratecore.xlib.XLibFactory;
import com.tools.acceleratecore.xlib.tool.intf.ITelephonyObserver;
import com.tools.acceleratecore.xlib.tool.intf.ITelephonyObserverListener;
import com.tools.acceleratecore.xlib.xlib.impl.XObserver;
import java.util.Iterator;

public class TelephonyObserver extends XObserver<ITelephonyObserverListener> implements ITelephonyObserver {
    private Context mContext = null;
    private boolean mInited = false;
    private TelephonyManager mTelephonyManager = null;
    private PhoneStateListener mPhoneStateListener = null;

    public TelephonyObserver() {
        this.mContext = XLibFactory.getApplication();
        this._init();
    }

    private void _init() {
        this.mTelephonyManager = (TelephonyManager)this.mContext.getSystemService("phone");
        this.mPhoneStateListener = new PhoneStateListener() {
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                ITelephonyObserverListener listener;
                Iterator var5;
                if (state == 0) {
                    synchronized(TelephonyObserver.this.mListListener) {
                        var5 = TelephonyObserver.this.getListListener().iterator();

                        while(var5.hasNext()) {
                            listener = (ITelephonyObserverListener)var5.next();
                            listener.onCallStateIdle();
                        }
                    }
                } else if (2 == state) {
                    synchronized(TelephonyObserver.this.mListListener) {
                        var5 = TelephonyObserver.this.getListListener().iterator();

                        while(var5.hasNext()) {
                            listener = (ITelephonyObserverListener)var5.next();
                            listener.onCallStateOffHook();
                        }
                    }
                } else if (1 == state) {
                    synchronized(TelephonyObserver.this.mListListener) {
                        var5 = TelephonyObserver.this.getListListener().iterator();

                        while(var5.hasNext()) {
                            listener = (ITelephonyObserverListener)var5.next();
                            listener.onCallStateRinging();
                        }
                    }
                }

            }
        };
    }

    public boolean startListen() {
        if (this.mInited) {
            return false;
        } else {
            this.mInited = true;
            this.mTelephonyManager.listen(this.mPhoneStateListener, 32);
            return true;
        }
    }

    public void stopListen() {
        if (this.mInited) {
            this.mInited = false;
            this.mTelephonyManager.listen(this.mPhoneStateListener, 0);
        }
    }

    public int getMissCallCount() {
        int nCount = 0;
        Cursor cursor = this.mContext.getContentResolver().query(Calls.CONTENT_URI, new String[]{"type"}, " type=? and new=?", new String[]{"3", "1"}, "date desc");
        if (cursor != null) {
            nCount = cursor.getCount();
            cursor.close();
        }

        return nCount;
    }
}
